/**
 * 轮播图列表组件
 *
 * 这个组件演示了如何使用我们创建的自定义Hook来获取和显示数据
 * 展示了React Query的加载状态、错误处理和数据展示
 */
import React from 'react';
import { useActiveBanners } from '../hooks/useBanners';

export const BannerList: React.FC = () => {
  // 使用自定义Hook获取轮播图数据
  const { data: banners, isLoading, error, isError } = useActiveBanners();

  // 加载状态
  if (isLoading) {
    return (
      <div className="flex justify-center items-center py-8">
        <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
        <span className="ml-2 text-gray-600">加载中...</span>
      </div>
    );
  }

  // 错误状态
  if (isError) {
    return (
      <div className="bg-red-50 border border-red-200 rounded-lg p-4">
        <div className="flex">
          <div className="flex-shrink-0">
            <svg className="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
              <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
            </svg>
          </div>
          <div className="ml-3">
            <h3 className="text-sm font-medium text-red-800">
              加载失败
            </h3>
            <div className="mt-2 text-sm text-red-700">
              <p>无法加载轮播图数据: {error?.message}</p>
            </div>
          </div>
        </div>
      </div>
    );
  }

  // 空数据状态
  if (!banners || banners.length === 0) {
    return (
      <div className="text-center py-8">
        <svg className="mx-auto h-12 w-12 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
        </svg>
        <h3 className="mt-2 text-sm font-medium text-gray-900">暂无轮播图</h3>
        <p className="mt-1 text-sm text-gray-500">还没有添加任何轮播图内容</p>
      </div>
    );
  }

  // 数据展示
  return (
    <div className="space-y-4">
      <h2 className="text-2xl font-bold text-gray-900">轮播图列表</h2>

      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        {banners.map((banner) => (
          <div key={banner.id} className="bg-white rounded-lg shadow-md overflow-hidden">
            {/* 图片 */}
            <div className="aspect-w-16 aspect-h-9">
              <img
                src={banner.image_url}
                alt={banner.title}
                className="w-full h-48 object-cover"
                onError={(e) => {
                  // 图片加载失败时显示占位图
                  const target = e.target as HTMLImageElement;
                  target.src = '';
                }}
              />
            </div>

            {/* 内容 */}
            <div className="p-4">
              <h3 className="text-lg font-semibold text-gray-900 mb-2">
                {banner.title}
              </h3>

              {banner.subtitle && (
                <p className="text-sm text-gray-600 mb-2">
                  {banner.subtitle}
                </p>
              )}

              {banner.description && (
                <p className="text-sm text-gray-500 line-clamp-3">
                  {banner.description}
                </p>
              )}

              {/* 链接 */}
              {banner.link_url && (
                <div className="mt-4">
                  <a
                    href={banner.link_url}
                    className="inline-flex items-center text-sm text-blue-600 hover:text-blue-800"
                  >
                    了解更多
                    <svg className="ml-1 w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                    </svg>
                  </a>
                </div>
              )}
            </div>
          </div>
        ))}
      </div>
    </div>
  );
};